package com.tensquare.base.service;

import com.tensquare.base.dao.BaseDao;
import com.tensquare.base.pojo.Label;
import entity.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Service
public class BaseService {
    @Autowired
    private BaseDao baseDao;
    @Autowired
    private IdWorker idWorker;
   //1. 列表数据
    public List<Label> findAll() {
        return baseDao.findAll();
    }
    //2.据idl列表数据
    public Label findOne(String labelId) {
        return baseDao.findById(labelId).get();
    }
     //3. 添加数据
    public void add(Label label) {
        label.setId(idWorker.nextId()+"");
        baseDao.save(label);
    }
    //4.修改数据
    public void update(Label label) {
        baseDao.save(label);
    }
    //5.删除数据
    public void dele(String labelId) {
        baseDao.deleteById(labelId);
    }
    //6.条件查询
    public void find(Label label) {

        Specification<Label> spec = createSpecification(label);
        baseDao.findAll(spec);
    }
    //7.生成查询条件(HQL:hibernate Query Language)  select dt.id,dt.name from Deptment dt;
    private Specification<Label> createSpecification(Label label) {
        return new Specification<Label>() {
            @Override
            //root：代表封装查询参数
            public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                //7.1)定义代表查询条件的集合
                List<Predicate> predicateList = new ArrayList<>();
                //7.2)向集合中添加查询条件
                if(StringUtils.isNotBlank(label.getLabelname())){       //代表标签名是否存在
                    predicateList.add(cb.like(root.get("labelname").as(String.class),"%" + label.getLabelname() + "%"));
                }
                if(StringUtils.isNotBlank(label.getState())){           //代表查询状态值
                    predicateList.add(cb.like(root.get("state").as(String.class),label.getState()));
                }
                if(StringUtils.isNotBlank(label.getRecommend())){
                    predicateList.add(cb.like(root.get("recommend").as(String.class),label.getRecommend()));
                }
                //7.3)定义数组
                Predicate[] predicates = new Predicate[predicateList.size()];
                //7.4)将集合放到数组中
                predicateList.toArray(predicates);
                //7.5)返回查询条件
                return cb.and(predicates);
            }
        };
    }
    //8.条件查询带分页
    public PageResult search(Label label, int page, int size) {
        //8.1)创建查询条件
        Specification<Label> spec = createSpecification(label);
        //8.2)开始进行条件查询
        Page<Label> labelPage = baseDao.findAll(spec, PageRequest.of(page - 1, size));
        //8.3)返回pageResult对象
        return new PageResult(labelPage.getTotalElements(),labelPage.getContent());
    }
}
